package com.qm.console.controller.rest;

import com.google.common.collect.ImmutableMap;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.controller.dto.ResultEnum;
import com.qm.console.dao.model.UserMessageDO;
import com.qm.console.pubdef.dto.user.UserMessageDTO;
import com.qm.console.pubdef.dto.user.UserMessageQueryDTO;
import com.qm.console.service.user.UserMessageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.controller.rest
 * Created on 2020/11/18.
 */
@RestController
@RequestMapping("/console/message")
@Api(value = "[ 推送消息API ]", description = "推送消息API")

public class UserMessageController {

    @Autowired
    private UserMessageService userMessageService;

    @PostMapping
    @ApiOperation("新建消息")
    public BaseRsp createMessage(@RequestBody UserMessageDTO userMessageDTO) {
        userMessageService.createMessage(userMessageDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping
    @ApiOperation("删除消息(删除已发送或正在发送的消息会失败)")
    public BaseRsp deleteMessage(@RequestBody List<Integer> uids) {
        userMessageService.deleteMessage(uids);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping
    @ApiOperation("修改消息(修改已发送或正在发送的消息会失败)")
    public BaseRsp updateMessage(@RequestBody UserMessageDO userMessageDO) {
        userMessageService.updateMessage(userMessageDO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping
    @ApiOperation("获取消息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "状态, -1: 用户id不正确, 0: 待发送 1: 发送中 2: 发送成功 3: 发送失败", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "targetUserId", value = "用户ID", dataType = "int", paramType = "query")})
    public BaseRsp getMessageList(UserMessageQueryDTO queryDTO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of(
                "list", userMessageService.getMessages(queryDTO),
                "count", userMessageService.getMessageCount(queryDTO)
        ));
    }

    @PostMapping("/push")
    public BaseRsp pushMessage(@RequestBody List<Integer> ids) {
        userMessageService.pushMessage(ids);
        return new BaseRsp(ResultEnum.SUCCESS);
    }
}
